Explanation of `self` usage during dealloc?
Posted
by Greg
on Stack Overflow
See other posts from Stack Overflow
or by Greg
Published on 2010-06-08T17:51:33Z
Indexed on
2010/06/08
18:42 UTC
Read the original article
Hit count: 249
iphone
|objective-c
I'm trying to lock down my understanding of proper memory management within Objective-C.
I've gotten into the habit of explicitly declaring self.myProperty
rather than just myProperty
because I was encountering occasional scenarios where a property would not be set to the reference that I intended.
Now, I'm reading Apple documentation on releasing IBOutlets
, and they say that all outlets should be set to nil
during dealloc
. So, I put this in place as follows and experienced crashes as a result:
- (void)dealloc {
[self.dataModel close];
[self.dataModel release], self.dataModel = nil;
[super dealloc];
}
So, I tried taking out the "self" references, like so:
- (void)dealloc {
[dataModel close];
[dataModel release], dataModel = nil;
[super dealloc];
}
This second system seems to work as expected. However, it has me a bit confused. Why would self
cause a crash in that case, when I thought self
was a fairly benign reference more used as a formality than anything else? Also, if self
is not appropriate in this case, then I have to ask: when should you include self
references, and when should you not?
© Stack Overflow or respective owner